package com.adabank.controller;

import com.adabank.dao.GuKeMapper;
import com.adabank.module.GuKe;
import com.adabank.module.GuKeExample;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSON;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequiresAuthentication
@RequestMapping("/api")
public class Customers {
    @Autowired

    final GuKeMapper guKeMapper;
    GuKeExample guKeExample;
    GuKeExample.Criteria criteria;

    public Customers(GuKeMapper guKeMapper) {
        this.guKeMapper = guKeMapper;
    }


//    @GetMapping("/me")
//    public Controller me() {
//        return Controller.of("Wonderland");
//    }

    //    搜索接口
    @GetMapping("/customers")
    public Controller<List<GuKe>> getAlii(String query) {
        guKeExample = new GuKeExample();
        criteria = guKeExample.createCriteria();

        JSONObject jsonObj = JSON.parseObject(query);

        List<GuKe> guKeList = new ArrayList<>();

        if (jsonObj.isEmpty()) {
            guKeList = getAllGuKe();
        }
        for (String key : jsonObj.keySet()) {
            switch (key) {
//                    卡号
                case "cardNumber":
                    guKeList = getCardNumber(jsonObj.getString(key));
                    break;
//                    最少余额
                case "minBalance":
                    guKeList = getMinBalance(jsonObj.getInteger(key));
                    break;
//                    最大余额
                case "maxBalance":
                    guKeList = getMaxBalance(jsonObj.getInteger(key));
                    break;
//                    公司名
                case "companyName":
                    guKeList = getCompanyName(jsonObj.getString(key));
                    break;
//                    公司地址
                case "companyAddress":
                    guKeList = getCompanyAddress(jsonObj.getString(key));
                    break;
//                    业务范围
                case "business":
                    guKeList = getBusiness(jsonObj.getString(key));
                    break;
//                    法人姓名
                case "legalRepName":
                    guKeList = getLegalRepName(jsonObj.getString(key));
                    break;
                default:
                    guKeList = getAllGuKe();
            }
        }
        return Controller.of(guKeList);
    }

    //    添加顾客成功
    @RequiresRoles(value = {"admin"})
    @PostMapping("/customers")
    public Controller<String> getEmployeesById(@RequestBody GuKe guKe) {
        guKeMapper.insert(guKe);
        return Controller.of("添加顾客成功 yes yes yes ");
    }

    //    删除顾客
    @RequiresRoles(value = {"admin"})
    @DeleteMapping("/customers/{id}")
    public Controller<String> del(@PathVariable Integer id) {
        guKeMapper.deleteByPrimaryKey(id);
        return Controller.of("删除成功");
    }

    //    更新顾客
    @RequiresRoles(value = {"admin"})
    @PostMapping("/customers/{id}")
    public Controller<String> updata(@PathVariable Integer id, @RequestBody GuKe guKe) {
        this.criteria.andIdEqualTo(id);
        guKeMapper.updateByExampleSelective(guKe, guKeExample);
        return Controller.of("del");
    }

    //                    法人姓名
    private List<GuKe> getLegalRepName(String string) {
        this.criteria.andLegalrepnameEqualTo(string);
        return guKeMapper.selectByExample(guKeExample);
    }

    //                    业务范围
    private List<GuKe> getBusiness(String string) {
        this.criteria.andBusinessEqualTo(string);
        return guKeMapper.selectByExample(guKeExample);

    }

    //                    公司地址
    private List<GuKe> getCompanyAddress(String string) {
        this.criteria.andCompanyaddressEqualTo(string);
        return guKeMapper.selectByExample(guKeExample);
    }

    //                    公司名
    private List<GuKe> getCompanyName(String string) {
        this.criteria.andCompanynameEqualTo(string);
        return guKeMapper.selectByExample(guKeExample);
    }

    //                    卡号
    private List<GuKe> getCardNumber(String string) {
        this.criteria.andCardnumberEqualTo(string);
        return guKeMapper.selectByExample(guKeExample);
    }

    //                    最大余额
    private List<GuKe> getMaxBalance(Integer num) {
        this.criteria.andBalanceLessThan(num);
        return guKeMapper.selectByExample(guKeExample);
    }

    //                    最少余额
    private List<GuKe> getMinBalance(Integer num) {
        this.criteria.andBalanceGreaterThan(num);
        return guKeMapper.selectByExample(guKeExample);
    }

    private List<GuKe> getAllGuKe() {
        return guKeMapper.selectByExample(guKeExample);
    }
}
